﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Dao;

namespace Web.Page
{
    public partial class CS_CWSX_Query : BasePage
    {
        ZL_CK_Dao daoZLCK = new ZL_CK_Dao();
        CS_CWSX_Dao daoCWSX = new CS_CWSX_Dao();

        protected void Page_Load(object sender, EventArgs e)
        {
            base.Page_Load(sender, e);
            if (!IsPostBack)
            {
                this.lbTitle.Text = "仓位属性列表";
                BindGridview();
            }
        }

        protected void GridView1_PageIndexChanged(object sender, EventArgs e)
        {
            BindGridview();
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            BindGridview();
            SavePageIndex(e.NewPageIndex);
        }

        private void BindGridview()
        {
            GridView1.PageIndex = RestorePageIndex();
            List<CS_CWSX> listCS_CWSX = daoCWSX.QueryAll();
            List<CS_CWSX_Ex> listEx = new List<CS_CWSX_Ex>();
            CS_CWSX_Ex ce;
            foreach (CS_CWSX c in listCS_CWSX)
            {
                ce = new CS_CWSX_Ex();
                ce.ID = c.ID.ToString();
                ce.CKID = c.CKID.ToString();
                ce.DM = c.DM;
                ce.BH = c.BH;
                if (c.CKID != null)
                {
                    ce.CKMC = daoZLCK.Query(c.CKID.ToString()).MC;
                }
                listEx.Add(ce);
            }


            this.GridView1.DataSource = listEx;
            this.GridView1.DataBind();
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //if (e.Row.RowType == DataControlRowType.DataRow)
            //{
            //    ((ImageButton)(e.Row.Cells[4].Controls[0])).Attributes.Add("onclick ", "return   confirm( '确定删除？ ') ");
            //}
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string id = GridView1.DataKeys[e.RowIndex].Value.ToString();
            int result = daoCWSX.Delete(id);
            if (result == 1)
            {
                lbResult.Text = "删除成功";
                BindGridview();
            }
        }

        protected class CS_CWSX_Ex
        {
            public string ID { get; set; }
            public string CKID { get; set; }
            public string DM { get; set; }
            public string BH { get; set; }
            public string CKMC { get; set; }
        }

        protected void btnAdd_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("~/Page/CS_CWSX_Add.aspx");
        }
    }
}